package o;

import android.content.Context;
import android.net.Uri;
import android.net.Uri.Builder;
import android.text.TextUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

public class bmj implements bmk {
    private final HashMap<String, bmo> ˋ = new HashMap();

    public void ˏ(String str, bmo o_bmo) {
        if (this.ˋ.containsKey(str)) {
            bmg.ˊ.ˊ("MyPathStrategy", "config has been added:" + str);
        } else if (TextUtils.isEmpty(str)) {
            bmg.ˊ.ˏ("MyPathStrategy", "error name,Name must not be empty");
        } else if (o_bmo == null || o_bmo.ˏ == null || TextUtils.isEmpty(o_bmo.ˋ)) {
            bmg.ˊ.ˏ("MyPathStrategy", "error rootConfig");
        } else {
            try {
                o_bmo.ˏ = o_bmo.ˏ.getCanonicalFile();
                this.ˋ.put(str, o_bmo);
            } catch (IOException e) {
                bmg.ˊ.ˏ("MyPathStrategy", "error rootFile");
            }
        }
    }

    public Uri ˏ(Context context, File file) {
        try {
            String canonicalPath = file.getCanonicalPath();
            Entry entry = null;
            for (Entry entry2 : this.ˋ.entrySet()) {
                Entry entry22;
                bmo o_bmo = (bmo) entry22.getValue();
                String path = o_bmo.ˏ.getPath();
                if (!(canonicalPath.startsWith(path) && canonicalPath.endsWith(o_bmo.ˋ) && (entry == null || path.length() > ((bmo) entry.getValue()).ˏ.getPath().length()))) {
                    entry22 = entry;
                }
                entry = entry22;
            }
            if (entry == null) {
                bmg.ˊ.ˏ("MyPathStrategy", "error specificEntry,Failed to find configured root that contains");
                return null;
            }
            String substring;
            String path2 = ((bmo) entry.getValue()).ˏ.getPath();
            if (path2.endsWith("/")) {
                substring = canonicalPath.substring(path2.length());
            } else {
                substring = canonicalPath.substring(path2.length() + 1);
            }
            return new Builder().scheme("content").authority(context.getPackageName() + ".storagedata.provider").encodedPath(Uri.encode((String) entry.getKey()) + '/' + Uri.encode(substring, "/")).build();
        } catch (IOException e) {
            bmg.ˊ.ˏ("MyPathStrategy", "error filePath,Failed to resolve canonical path for wisedist");
            return null;
        }
    }

    public File ˊ(Uri uri) {
        String encodedPath = uri.getEncodedPath();
        int indexOf = encodedPath.indexOf(47, 1);
        String decode = Uri.decode(encodedPath.substring(1, indexOf));
        String decode2 = Uri.decode(encodedPath.substring(indexOf + 1));
        bmo o_bmo = (bmo) this.ˋ.get(decode);
        if (o_bmo == null) {
            bmg.ˊ.ˏ("MyPathStrategy", "Unable to find configured root for");
            return null;
        }
        try {
            File canonicalFile = new File(o_bmo.ˏ, decode2).getCanonicalFile();
            if (!canonicalFile.getPath().startsWith(o_bmo.ˏ.getPath())) {
                bmg.ˊ.ˏ("MyPathStrategy", "Resolved path jumped beyond configured root");
                return null;
            } else if (canonicalFile.getPath().endsWith(o_bmo.ˋ)) {
                return canonicalFile;
            } else {
                bmg.ˊ.ˏ("MyPathStrategy", "Resolved path error suffix");
                return null;
            }
        } catch (IOException e) {
            bmg.ˊ.ˏ("MyPathStrategy", "error uriPath,Failed to resolve canonical path for");
            return null;
        }
    }
}
